package com.demo.thrift.base;

import java.util.HashMap;

import shared.SharedStruct;
import tutorial.InvalidOperation;
import tutorial.Work;
import tutorial.Calculator;

public class CalculatorHandler implements Calculator.Iface {

	  private HashMap<Integer,SharedStruct> log;

	  public CalculatorHandler() {
	    log = new HashMap<Integer, SharedStruct>();
	  }

	  public void ping() {
	    System.out.println("ping()");
	  }

	  public int add(int n1, int n2) {
	    System.out.println("add(" + n1 + "," + n2 + ")");
	    return n1 + n2;
	  }

	  public int calculate(int logid, Work work) throws InvalidOperation {
	    System.out.println("calculate(" + logid + ", {" + work.op + "," + work.num1 + "," + work.num2 + "})");
	    int val = 0;
	    switch (work.op) {
	    case ADD:
	      val = work.num1 + work.num2;
	      break;
	    case SUBTRACT:
	      val = work.num1 - work.num2;
	      break;
	    case MULTIPLY:
	      val = work.num1 * work.num2;
	      break;
	    case DIVIDE:
	      if (work.num2 == 0) {
	        InvalidOperation io = new InvalidOperation();
	        io.whatOp = work.op.getValue();
	        io.why = "Cannot divide by 0";
	        throw io;
	      }
	      val = work.num1 / work.num2;
	      break;
	    default:
	      InvalidOperation io = new InvalidOperation();
	      io.whatOp = work.op.getValue();
	      io.why = "Unknown operation";
	      throw io;
	    }

	    SharedStruct entry = new SharedStruct();
	    entry.key = logid;
	    entry.value = Integer.toString(val);
	    log.put(logid, entry);

	    return val;
	  }

	  public SharedStruct getStruct(int key) {
	    System.out.println("getStruct(" + key + ")");
	    return log.get(key);
	  }

	  public void zip() {
	    System.out.println("zip()");
	  }

	}
